﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AirlineBooking.Models.DTO;

namespace AirlineBooking.Models
{
    public class AirportModel
    {
        public List<DTOAirport> GetAirportList()
        {
            using (AirLineDBModelEntities db = new AirLineDBModelEntities())
            {
                var query = db.AirPorts.Select(i => new DTOAirport() { Code = i.Code, Name = i.Name, CityName = i.City1.Name, ServiceFee = i.ServiceFee ?? 0});
                return query.ToList();
            }
        }

        public List<DTOAirport> GetCityAirports(int cityId)
        {
            using (AirLineDBModelEntities db = new AirLineDBModelEntities())
            {
                City city = db.Cities.FirstOrDefault(i => i.Id == cityId);
                if (city == null)
                {
                    return null;
                }
                var result = city.AirPorts.Select(i => new DTOAirport() { Code = i.Code, Name = i.Name, ServiceFee = i.ServiceFee ?? 0 });
                return result.ToList();
            }
        }

        public DTOAirport GetAirport(string AirportCode)
        {
            using (AirLineDBModelEntities db = new AirLineDBModelEntities())
            {
                AirPort airport = db.AirPorts.FirstOrDefault(i => i.Code == AirportCode);
                if (airport == null)
                {
                    return null;
                }
                DTOAirport result = new DTOAirport() { Code = airport.Code, Name = airport.Name, ServiceFee = airport.ServiceFee ?? 0 };
                return result;
            }
        }
    }
}